package com.hedl.resp;

import java.io.Serializable;
import java.util.HashMap;

public class BaseResponse extends HashMap<String, Object> implements Serializable{
    public static final Integer SUCCESS_CODE = 200;
    public static final Integer BAD_REQ_CODE = 400;
    public static final Integer NO_AUTH_CODE = 401;
    public static final Integer NOT_FOUND_CODE = 404;
    public static final Integer BAD_REQ_TYPE_CODE = 405;
    public static final Integer SYS_ERR_CODE = 500;
    public static final Integer PARAM_ERR_CODE = 403;
    public static final String CODE_KEY = "code";
    public static final String ERR_MSG_KEY = "errMsg";
    public static final String IS_SUCCESS_KEY = "success";
    public static final String DATA_KEY = "data";

    public static BaseResponse getInstance(){
        return new BaseResponse();
    }

    public BaseResponse setCode(Integer code){
        this.putData(CODE_KEY, code);
        return this;
    }

    public BaseResponse setIsSuccess(Boolean isSuccess){
        this.putData(IS_SUCCESS_KEY, isSuccess);
        return this;
    }

    public <T> BaseResponse setData(T data){
        this.putData(DATA_KEY, data);
        return this;
    }

    public BaseResponse setErrMsg(String errMsg){
        this.putData(ERR_MSG_KEY, errMsg);
        return this;
    }

    public <T> BaseResponse putData(String key, T data){
        this.put(key, data);
        return this;
    }

    public BaseResponse success(){
        this.put(IS_SUCCESS_KEY, true);
        this.put(CODE_KEY, SUCCESS_CODE);
        return this;
    }

    public <T> BaseResponse success(T data){
        BaseResponse instance = success();
        instance.put(DATA_KEY, data);
        return instance;
    }

    public <T> BaseResponse success(T data, String msg){
        BaseResponse instance = success(data);
        instance.put(ERR_MSG_KEY, msg);
        return instance;
    }


    public BaseResponse error(Integer code){
        BaseResponse instance = new BaseResponse();
        instance.put(IS_SUCCESS_KEY, false);
        instance.put(CODE_KEY, code);
        instance.put(ERR_MSG_KEY, "error-"+code);
        return instance;
    }

    public BaseResponse error(Integer code, String msg){
        BaseResponse instance = error(code);
        instance.put(ERR_MSG_KEY, msg);
        return instance;
    }

    public <T> BaseResponse error(Integer code, String msg, T errData){
        BaseResponse instance = error(code, msg);
        instance.put(DATA_KEY, errData);
        return instance;
    }
}
